package project;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: jc218934
 * Date: 06/04/2011
 * Time: 10:30:07 AM
 */
public class YahooJapanTranslatedUrl
{
    private URL url;
    private String googleTranslateUrl = "http://translate.google.com.au/translate?hl=en&sl=ja&tl=en&u=";
    public YahooJapanTranslatedUrl(String url)
    {
        try
        {
            this.url = new URL(transformStringUrlToGoogleTranslateUrlOfUrl(url));
        }
        catch (MalformedURLException e)
        {
            this.url = null;
        }
    }

    private String transformStringUrlToGoogleTranslateUrlOfUrl (String urlString)
    {
        urlString = urlString.replaceAll("%", "%25"); //replace % by %25
        urlString = urlString.replaceAll(":", "%3A"); //replace : by %3A
        urlString = urlString.replaceAll("/", "%2F"); //replace / by %2F
        urlString = urlString.replaceAll("\\?", "%3F"); //replace ? by %3F
        urlString = urlString.replaceAll("=", "%3D"); //replace = by %3D
        urlString = urlString.replaceAll("&", "%26"); //replace & by %26

        urlString = googleTranslateUrl + urlString;
        return urlString;
    }

    public URL getUrl()
    {
        return url;
    }

}
